package atessitura;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.BoxLayout;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;

import java.awt.Component;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

//import org.newdawn.slick.CanvasGameContainer;
//import org.newdawn.slick.SlickException;

import custom.BackgroundPanel;

import net.miginfocom.swing.MigLayout;

import java.awt.Color;


public class TFrame extends JFrame {

	private BackgroundPanel background;
	private TFController control;
	
	private GuitarPanel gPanel;
	//private SGuitarPanel sgPanel;
	private ControlPanel cPanel;
	
	JMenuItem mntmPlay;
	
	private JLabel songName;
	private JLabel noteName;
	

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TFrame frame = new TFrame();
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 */
	public TFrame(TFController controller) {
		super("Tessitura");
		control=controller;
		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 500);
		//setExtendedState(JFrame.MAXIMIZED_BOTH);
		setVisible(true);
		
		songName=new JLabel("hi y'all");
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmNew = new JMenuItem("New");
		mntmNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				control.menuNew();
			}
		});
		mnFile.add(mntmNew);
		
		JMenuItem mntmOpen = new JMenuItem("Open...");
		mntmOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				control.menuOpen();
			}
		});
		mnFile.add(mntmOpen);
		
		JMenuItem mntmOpenExampleSongs = new JMenuItem("Open Example Songs...");
		mntmOpenExampleSongs.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				control.menuOpenExample();
			}
		});
		mnFile.add(mntmOpenExampleSongs);
		
		JMenuItem mntmSave = new JMenuItem ("Save");
		mntmSave.addActionListener (new ActionListener () {
			public void actionPerformed (ActionEvent arg0){
				try {
					control.save();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		mnFile.add(mntmSave);
		
		JMenuItem mntmImport = new JMenuItem("Import");
		mntmImport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					control.menuImport();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		mntmImport.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK));
	    mnFile.add(mntmImport);
		
		JMenu mnExport = new JMenu("Export");
		mnFile.add(mnExport);
		
		JMenuItem mntmMidi = new JMenuItem("MIDI");
		mntmMidi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				control.menuExportMIDI();
			}
		});
		mnExport.add(mntmMidi);
		
		JMenuItem mntmMp = new JMenuItem("MP3");
		mntmMp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				control.menuExportMP3();
			}
		});
		mnExport.add(mntmMp);
		
		JMenuItem mntmWav = new JMenuItem("WAV");
		mntmWav.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				control.menuExportWAV();
			}
		});
		mnExport.add(mntmWav);
		
		mnFile.addSeparator();
		
		JMenuItem mntmPreferences = new JMenuItem("Preferences");
		mntmPreferences.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				control.menuPreferences();
			}
		});
		mnFile.add(mntmPreferences);
		
		JMenuItem mntmScoreInformation = new JMenuItem("Score Information");
		mntmScoreInformation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				control.menuScoreInfo();
			}
		});
		mnFile.add(mntmScoreInformation);
		
		JMenuItem mntmPrint = new JMenuItem("Print");
		mntmPrint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		mnFile.add(mntmPrint);
		
		JMenu mnEdit = new JMenu("Edit");
		menuBar.add(mnEdit);
		
		JMenu mnEffects = new JMenu("Effects");
		menuBar.add(mnEffects);
		
		JMenu mnSound = new JMenu("Sound");
		menuBar.add(mnSound);
		
		mntmPlay = new JMenuItem("Play");
		mntmPlay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				control.play();
			}
		});
		mntmPlay.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0));
		mnSound.add(mntmPlay);
		
		
		
		JMenuItem mntmStop = new JMenuItem("Stop");
		mntmStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				control.stop();
			}
		});
		mnSound.add(mntmStop);
		
		JMenuItem mntmRewind = new JMenuItem("Rewind");
		mntmRewind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				control.rewind();
			}
		});
		mnSound.add(mntmRewind);
		
		mnSound.addSeparator();
		
		JMenuItem mntmMetronome = new JMenuItem("Metronome");
		mntmMetronome.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				control.metronome();
			}
		});
		mnSound.add(mntmMetronome);
		
		JMenuItem mntmTuner = new JMenuItem("Tuner");
		mntmTuner.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				control.tuner();
			}
		});
		mnSound.add(mntmTuner);
		
		JMenu mnView = new JMenu("View");
		menuBar.add(mnView);
		
		JMenuItem mntmPagesPerColumn = new JMenuItem("Pages(1 per Column)");
		mntmPagesPerColumn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//control.menuPagesOne(); or maybe control.menuPages(1);?
			}
		});
		mnView.add(mntmPagesPerColumn);
		
		JMenuItem mntmPagesPerColumn_1 = new JMenuItem("Pages(2 per Column)");
		mntmPagesPerColumn_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		mnView.add(mntmPagesPerColumn_1);
		
		JMenu mnOrientation = new JMenu("Orientation");
		mnView.add(mnOrientation);
		
		JMenuItem mntmVertical = new JMenuItem("Vertical");
		mntmVertical.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		mnOrientation.add(mntmVertical);
		
		JMenuItem mntmHorizontal = new JMenuItem("Horizontal");
		mntmHorizontal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		mnOrientation.add(mntmHorizontal);
		
		BufferedImage carbonFibre = null;
		try {
			carbonFibre = ImageIO.read(TFrame.class.getResource("/resources/images/carbon_fibre_big.png"));
		} catch (IOException e) {
		}
		
		background=new BackgroundPanel(carbonFibre);
		background.setStyle(1);

		setContentPane(background);
		GridBagLayout gridbag = new GridBagLayout();
		MigLayout miglayout=new MigLayout("fill");
		//BorderLayout borderlayout=new BorderLayout();
		background.setLayout(miglayout);
		
		VolumePanel vpanel=new VolumePanel();
		//background.add(vpanel,"wrap, grow");
		
		cPanel=new ControlPanel(control);
		background.add(cPanel,"wrap, grow");
		
		gPanel=new GuitarPanel(control);
		background.add(gPanel,"wrap, grow");
		
		/*try {
			sgPanel = new SGuitarPanel(control);
			CanvasGameContainer sgContainer=new CanvasGameContainer(sgPanel);
			background.add(sgContainer, "grow, shrink");
			sgContainer.setBounds(0, 0, 550, 300);
			sgContainer.start();
		} catch (SlickException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/
	}
	
	public void setNoteName (String note)
	{
	
		//noteName.setText("Note: " + note);
	}
	
	public void setNotes(ArrayList<GuitarNote>[] notes)
	{
		gPanel.setNotes(notes);
		//sgPanel.setNotes(notes);
	}
	
	public void setSongName(String songNameString)
	{
		songName.setText(songNameString);
	}
	
	public void playpauseswitch(boolean play)
	{
		if (!play)
		{
			mntmPlay.setText("Pause");
			return;
		}
		mntmPlay.setText("Play");
		repaint();
	}
	
	public void repaintGPanel()
	{
		gPanel.repaint();
	}
	
	public void repaintCPanel()
	{
		cPanel.repaint();
	}
	
	public void setUpCPanel()
	{
		cPanel.setUpTracks();
		cPanel.setTimes();
		cPanel.resetDifficulty();
	}

	public void updateTime(double percentPlayed) {
		cPanel.updateTime(percentPlayed);
		
	}
	
	public void setTempoFactor(double tempoFactor) {
		gPanel.setTempoFactor(tempoFactor);
		//sgPanel.setTempoFactor(tempoFactor);
		
	}

}
